/***************************************************************************
 *   Copyright (C) 2007 by ipQalc team                                     *
 *   milambert@c-webhosting.org                                            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <QtGui>
#include <QDebug>
#include "infoip.h"

InfoIpDialog::InfoIpDialog(QWidget *parent) : QWidget(parent){
    //QGroupBox *infoGroup = new QGroupBox(tr("infoIp"), this);
    QVBoxLayout *layout = new QVBoxLayout;

    // IP input
    QLabel *ipLabel = new QLabel(tr("IP: "));
    ipEdit = new QLineEdit;
    // QValidator *ipVal = new QIntValidator(0, 255, this);
    ipEdit->setInputMask("999.999.999.999;");
    // ipEdit->setText("000000000000");
    // ipEdit->setCursorPosition(0);
    // ipEdit->setValidator(new QIntValidator(255));

    // Mask input
    QLabel *maskLabel = new QLabel(tr("Mask: "));
    maskEdit = new QLineEdit;
    maskEdit->setInputMask("999.999.999.999; ");
    // maskEdit->setText("000000000000");
    maskEdit->setCursorPosition(1);

    // set and reset button and signals
    QPushButton *set = new QPushButton(tr("Get Info"));
    QPushButton *reset = new QPushButton(tr("Reset"));

    connect(set, SIGNAL(clicked()), this, SLOT(getInfo()));
    connect(reset, SIGNAL(clicked()), this, SLOT(reset()));

    // network adress Result
    QLabel *netLabel = new QLabel(tr("Net adr: "));
    netResult = new QLabel;
    netResult->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
    netResult->setMinimumSize(200, 22);

    // broadcast adress Result
    QLabel *bcLabel = new QLabel(tr("broadcast: "));
    bcResult = new QLabel;
    bcResult->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
    bcResult->setMinimumHeight(22);

    // class network result
    QLabel *classLabel = new QLabel(tr("class: "));
    classNet = new QLabel;
    classNet->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
    classNet->setMinimumHeight(22);

    // nb host Result
    QLabel *nbHostLabel = new QLabel(tr("nb Host: "));
    nbHost = new QLabel;
    nbHost->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
    nbHost->setMinimumHeight(22);

    // nb subnet result
    QLabel *nbSubLabel = new QLabel(tr("nb Subnet: "));
    nbSubnet = new QLabel;
    nbSubnet->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
    nbSubnet->setMinimumHeight(22);

    // lineEdit layout
    QGridLayout *editLayout = new QGridLayout;
    editLayout->addWidget(ipLabel, 0, 0);
    editLayout->addWidget(ipEdit, 0, 1);
    editLayout->addWidget(maskLabel, 1, 0);
    editLayout->addWidget(maskEdit, 1, 1);
    editLayout->addWidget(set, 0, 2);
    editLayout->addWidget(reset, 1, 2);

    // result layout
    QGridLayout *resultLayout = new QGridLayout;
    resultLayout->addWidget(netLabel, 0, 0);
    resultLayout->addWidget(netResult, 0, 1);
    resultLayout->addWidget(bcLabel, 1, 0);
    resultLayout->addWidget(bcResult, 1, 1);
    resultLayout->addWidget(classLabel, 2, 0);
    resultLayout->addWidget(classNet, 2, 1);
    resultLayout->addWidget(nbHostLabel, 3, 0);
    resultLayout->addWidget(nbHost, 3, 1);
    resultLayout->addWidget(nbSubLabel, 4, 0);
    resultLayout->addWidget(nbSubnet, 4, 1);
    resultLayout->setRowStretch(5, 1);
    resultLayout->setColumnStretch(2, 1);

    // main layout
    layout->addLayout(editLayout, 0);
    layout->addLayout(resultLayout, 1);
    //infoGroup->setLayout(layout);
    this->setLayout(layout);
}

void InfoIpDialog::getInfo(){

    ipAdress = new IpAdress(ipEdit->text());

    if (ipAdress->isValid() && ipAdress->isValid(maskEdit->text())){
        netResult->setText(ipAdress->getNetAdress(maskEdit->text()));
        classNet->setText(ipAdress->getClassAdr());
        nbHost->setText(ipAdress->getNbHost());
        nbSubnet->setText(ipAdress->getNbSubnet());
    } else
        QMessageBox::warning(this, tr("Error"), tr("Invalid ip or mask adress"));
}

void InfoIpDialog::reset(){
    netResult->clear();
    bcResult->clear();
    classNet->clear();
    nbHost->clear();
    nbSubnet->clear();
}

InfoIpDialog::~InfoIpDialog(){}
